package com.cloudkinto.controller.order;


import com.cloudkinto.base.BaseController;
import com.cloudkinto.bo.order.OrderAmazonQueryBo;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.service.order.OrderAmazonService;
import com.cloudkinto.service.order.vo.Export2ShipOrderReq;
import com.cloudkinto.service.order.vo.amazon.AmazonOrderDetailRes;
import com.cloudkinto.service.order.vo.amazon.AmazonOrderDownReq;
import com.cloudkinto.service.order.vo.amazon.UpdateSysMemoReq;
import com.cloudkinto.service.orderoutput.vo.OrderOutputAddReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 张永远
 * @since 2020-06-02
 */
@RestController
@Api(tags = "亚马逊订单模块")
@RequestMapping("/api/orderAmazon")
@RequiredArgsConstructor
public class OrderAmazonController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(OrderAmazonController.class);

    private final OrderAmazonService service;

    @UserSessionRequired
    @GetMapping("/downloadInit")
    @ApiOperation(value = "店铺初始化", notes = "初始化")
    public SingleResult addInit() {
        return service.addInit(getCurrentUserId(), getCompanyId());
    }

    @UserSessionRequired
    @PostMapping("downloadAmazonOrders")
    @ApiOperation(value = "下载亚马逊订单", notes = "下载亚马逊订单")
    public SingleResult add(@RequestBody @Validated AmazonOrderDownReq req) throws Exception {
        Long userId = getCurrentUserId();
        Long companyId = getCompanyId();
        return service.downloadAmazonOrders(req, userId, companyId);
    }

    @UserSessionRequired
    @PostMapping("exportExcel")
    @ApiOperation(value = "导出所有数据excel")
    public void exportAll(@RequestBody OrderAmazonQueryBo bo, HttpServletResponse response) throws Exception {
        Long companyId = getCompanyId();
        if (Objects.nonNull(companyId)) {
            bo.setCompanyId(((companyId)));
        }
        if (!StringUtils.isEmpty(bo.getShopId())) {

        } else if (getShopId() != null) {
            bo.setShopId(getShopId() + "");
        }
        service.exportExcel(bo, response);
    }

    @UserSessionRequired
    @PostMapping("mark/sendOut")
    @ApiOperation(value = "标记为发货", notes = "标记为发货")
    public SingleResult markSendOut(@RequestBody List<Long> ids) throws Exception {
        Long userId = getCurrentUserId();
        Long companyId = getCompanyId();
        return service.markSendOut(ids, userId);
    }

    @UserSessionRequired
    @PutMapping("systemMemo")
    @ApiOperation(value = "编辑系统备注", notes = "")
    public SingleResult systemMemo(@RequestBody @Validated UpdateSysMemoReq req) throws Exception {
        Long userId = getCurrentUserId();
        Long companyId = getCompanyId();
        return service.updateSysMemo(req, userId, companyId);
    }

    @UserSessionRequired
    @GetMapping("systemMemoInit")
    @ApiOperation(value = "编辑系统备注初始化", notes = "")
    public SingleResult systemMemoInit(@RequestParam Long id) throws Exception {
        return service.systemMemoInit(id);
    }

    @UserSessionRequired
    @PostMapping("sendOut")
    @ApiOperation(value = "发货", notes = "发货")
    public SingleResult sendOut(@RequestBody Export2ShipOrderReq req) throws Exception {
        Long userId = getCurrentUserId();
        Long companyId = getCompanyId();
        if (req.getIsSolo() == 0) {
            return service.sendOut(req.getOrderids(), userId, companyId, false);
        } else {
            return service.sendOutNopackage(req.getOrderids()[0], userId, companyId);
        }
    }

    @UserSessionRequired
    @PostMapping("sendOutMatch")
    @ApiOperation(value = "确认发货发货", notes = "确认发货发货")
    public SingleResult sendOutMatch(@RequestBody List<OrderOutputAddReq> orderOutputAddReqList) throws Exception {
        Long userId = getCurrentUserId();
        return service.sendOutMatch(orderOutputAddReqList, userId);
    }


    @UserSessionRequired
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查看详情", notes = "根据id查看详情")
    @ApiResponses({@ApiResponse(code = 200, message = "", response = AmazonOrderDetailRes.class)})
    public SingleResult detail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功", service.detail(id));
    }

    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "查询初始化", notes = "查询初始化")
    public SingleResult pageInit() {
        return service.pageInit();
    }

    @UserSessionRequired
    @GetMapping("/listPage")
    @ApiOperation(value = "查询（分页）", notes = "查询（分页）")
    public PageResult listPage(OrderAmazonQueryBo bo) {
        Long companyId = getCompanyId();
        if (Objects.nonNull(companyId)) {
            bo.setCompanyId((companyId));
        }
        if (!StringUtils.isEmpty(bo.getShopId())) {

        } else if (getShopId() != null) {
            bo.setShopId(getShopId() + "");
        }
        return service.getListPage(bo);
    }

}

